package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.OrderSettingDao;
import com.atguigu.pojo.OrderSetting;
import com.atguigu.service.OrderSettingService;
import com.atguigu.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    OrderSettingDao orderSettingDao;

    @Override
    public void addOrUpdate(List<OrderSetting> list) {
        for (OrderSetting orderSetting : list) {
            OrderSetting orderSetting1 = orderSettingDao.findGetByOrderDate(orderSetting.getOrderDate());

            if(orderSetting1 == null){
                orderSettingDao.add(orderSetting);
            }else{
                orderSettingDao.update(orderSetting);
            }

        }
    }

    @Override
    public List<Map<String, Integer>> findPage(String date) {

        String start = date + "-01";
        String end = DateUtils.getLastDayOfMonth(date);

        Map<String, String> map = new HashMap<>();
        map.put("start",start);
        map.put("end",end);

        List<OrderSetting> list = orderSettingDao.findPage(map);

        List<Map<String, Integer>> list1 = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map<String, Integer> map1 = new HashMap<>();
            map1.put("date",orderSetting.getOrderDate().getDate());
            map1.put("number",orderSetting.getNumber());
            map1.put("reservations",orderSetting.getReservations());
            list1.add(map1);
        }

        return list1;
    }

    @Override
    public void addOrUpdateOrderSetting(OrderSetting orderSetting) {
        OrderSetting getByOrderDate = orderSettingDao.findGetByOrderDate(orderSetting.getOrderDate());

        if(getByOrderDate == null){
            orderSettingDao.add(orderSetting);
        }else{
            orderSettingDao.update1(orderSetting);
        }
    }
}
